package com.thinkgem.jeesite.common.task;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.ems.service.EmsStudentService;

// 数据导出定时任务
// @Component("exportDBTask")
public class ExportDBTask {
	
	public ExportDBTask()
	{
		System.out.println("被实例化---------------------");
	}
	
	/*@Autowired
	private EmsStudentService emsStudentService;*/
	
	//@Scheduled(cron = "0 27 18 * * ?")
	public void exportDB()
	{
		//获取下月月份
		/*String month = DateUtils.getAnyMonth(new Date(), 1);
		Map<String,String> param = new HashMap<String,String>();
		param.put("month", month);
		this.emsStudentService.createStuNoSeq(param);*/
		// System.out.println("======创建下月学号生成序列成功======");
		
		//List<String> logList = new ArrayList<String>();
		// response.setContentType("text/html; charset=utf-8"); 
		String spiderBatPath = Global.getConfig("export.bat.path");
		System.out.println(spiderBatPath);
		String exportLogPath = Global.getConfig("export.log.path");
		File logFile = new File(exportLogPath);
		PrintWriter pw = null;
		final Map<String,Object> rs = new HashMap<String,Object>();
		rs.put("success", false);
		// 每次请求，清空日志列表
		String startLine = "=========================导出开始:" + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "==========================";
		// logList = new ArrayList<String>();
		// logList.add(startLine);
		if(logFile.exists())
		{
			try {
				pw = new PrintWriter(new FileWriter(logFile),true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else
		{
			try {
				logFile.createNewFile();
				pw = new PrintWriter(new FileWriter(logFile),true);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		try {
			
            Process ps = Runtime.getRuntime().exec("cmd  /c " + spiderBatPath);
            InputStream in = ps.getErrorStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"gbk"));
            String line;
            pw.write(startLine);
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
                if(null != pw)
                {
                	pw.write(line + "\n");
                	pw.flush();
                	// logList.add(line);
                }
            }
            String endLine = "=========================数据导出完毕，导出成功。 " + DateUtils.getDate("yyyy-MM-dd HH:mm:ss") + "==========================";
            pw.write(endLine);
            // logList.add(endLine);
            in.close();
            pw.close();
            ps.waitFor();
            // rs.put("success", true);
            // rs.put("massage", "数据导出完毕，导出成功。");

        } catch (IOException ioe) {
            ioe.printStackTrace();
            // rs.put("massage", "数据导出异常：" + ioe.getMessage());
        } catch (InterruptedException e) {
            e.printStackTrace();
            // rs.put("massage", "数据导出异常：" + e.getMessage());
        }
        System.out.println("thread done");
	}
	
}
